package wwk.common.widget;

import java.util.List;
import wwk.common.util.LayoutUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.database.Cursor;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

public class CustomDialog implements DialogInterface.OnClickListener
{
	 private final  int KViewSpacingLeft=25;
	 private final  int KViewSpacingTop=10; 
	 private final  int KViewSpacingRight=25;
	 private final  int KViewSpacingBottom=10;
     
	 private Context context;
     private Builder builder;
	 private AlertDialog alertDialog;
	 private int buttonCount;
	 private DialogContent simpleDialogContent;//锟津单的对伙拷锟斤拷锟斤拷锟斤拷
	 private Handler handler;
	 private int clickedIndex;
	 
	 public CustomDialog(Context context) 
	 {
		 this.context=context;
		 builder=new AlertDialog.Builder(context);
	 }
	 
	 public CustomDialog(Activity activity) 
 {
	 	context=activity;
	    if (activity.getParent() != null) 
	    {
	    		context = activity.getParent(); 
	    }
	    
	    builder=new AlertDialog.Builder(context);
	 }
	 
	 private AlertDialog alertDialog()
	 {
		 if(alertDialog==null)
	     {
			 alertDialog=builder.create();
	     }
		 return alertDialog;
	 }
 
	 static public void showAlertDialogWithBlock(Context context,String title)
	 {
		 CustomDialog dialog=new CustomDialog(context);
		 dialog.setTitle(title);
		 dialog.setIcon(android.R.drawable.ic_menu_info_details);
		 dialog.addButtonOk();
		 dialog.showWithBlock();
	 }
	 
	 static public void showAlertDialog(Context context,String title)
	 {
		 CustomDialog dialog=new CustomDialog(context);
		 dialog.setTitle(title);
		 dialog.setIcon(android.R.drawable.ic_menu_info_details);
		 dialog.addButtonOk();
		 dialog.show();
	 }
	 
	 static public boolean showSelectDialog(Context context,String title,String message)
	 {
		 CustomDialog dialog=new CustomDialog(context);
		 dialog.setIcon(android.R.drawable.ic_menu_help);
		 dialog.setTitle(title);
		 dialog.setMessage(message);
		 dialog.addButtonOk();
		 dialog.addButtonCancel();
		 int buttonIndex=dialog.showWithBlock();
		 if(buttonIndex==0)
		 {
			 return true;
		 }
	 
		 return false;
	 }
	 
	 static public boolean showSelectDialog(Context context,String title)
	 {
		 CustomDialog dialog=new CustomDialog(context);
		 //dialog.setIcon(android.R.drawable.ic_menu_help);
		 dialog.setTitle(title);
		 dialog.addButtonOk();
		 dialog.addButtonCancel();
		 int buttonIndex=dialog.showWithBlock();
		 if(buttonIndex==0)
		 {
			 return true;
		 }
	 
		 return false;
	 }
	 
	 static public void showInfoDialog(Context context,String title,int icon,String message)
	 {
		 CustomDialog dialog=new CustomDialog(context);
		 dialog.setTitle(title);
		 dialog.setIcon(icon);
		 dialog.setMessage(message);
		 dialog.addButtonOk();
		 dialog.show();
	 }
	 
	 static public boolean showQueryDialog(Context context,String title,int icon,StringBuffer inOutStringBuffer)
	 {
		 CustomDialog dialog=new CustomDialog(context);
		 dialog.setTitle(title);
		 dialog.setIcon(icon);
		 EditText textView=new EditText(context);
		 textView.setText(inOutStringBuffer);
		 textView.setLayoutParams(LayoutUtil.getLayoutParamsFillParent());
		 dialog.setContentView(textView);
		 dialog.addButtonOk();
		 dialog.addButtonCancel();
		 int buttonIndex=dialog.showWithBlock(textView);
		 if(buttonIndex==0)
		 {
			 //get text
			 String text=textView.getText().toString();
			 inOutStringBuffer.setLength(0);
			 inOutStringBuffer.append(text);
			 
			 return true;
		 }
	 
		 return false;
	 }
	 
	 /**
	     * 锟斤拷选Dialog
	     */
	 static public boolean showMultiChoiceDialog(Context context,String title,String[] itemArray,final boolean[] stateArray)
	 {
		 CustomDialog dialog=new CustomDialog(context);
		 dialog.setTitle(title);
		 dialog.setMultiChoiceItems(itemArray, stateArray, new DialogInterface.OnMultiChoiceClickListener() 
		 {
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) 
			{
				stateArray[which]=isChecked;
			}
		});
		 
		 dialog.addButtonOk();
		 dialog.addButtonCancel();
		 int buttonIndex=dialog.showWithBlock();
		 if(buttonIndex==0)
		 {
			 return true;
		 }
	 
		 return false;
	 }
	 
	 /**
	     * 锟斤拷选Dialog
	     */
	 static public boolean showMultiChoiceDialog(Context context,String title,Cursor cursor,String isCheckedColumn, String labelColumn,final List<Integer> selectedIndexList)
	 {
		 CustomDialog dialog=new CustomDialog(context);
		 dialog.setTitle(title);
		 dialog.setMultiChoiceItems(cursor, isCheckedColumn,labelColumn, new DialogInterface.OnMultiChoiceClickListener() 
		 {
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) 
			{
				if(selectedIndexList==null)
				{
					return;
				}
				
				if(isChecked)
				{
					selectedIndexList.add(Integer.valueOf(which));
				}
				else
				{
					selectedIndexList.remove(Integer.valueOf(which));
				}
			}
		});
		 
		 dialog.addButtonOk();
		 dialog.addButtonCancel();
		 int buttonIndex=dialog.showWithBlock();
		 if(buttonIndex==0)
		 {
			 return true;
		 }
	 
		 return false;
	 }
	 
	 public void setTitle(String title)
	 {
		 if(title!=null)
		 {
			 builder.setTitle(title);
		 }
	 }
	 
	 public void setIcon(int icon)
	 {
		 builder.setIcon(icon);
	 }
	 
	 public void setMessage(String message)
	 {
		 if(alertDialog==null)
	     {
		   alertDialog=builder.create();
	     }
		 
		 if(message!=null)
		 {
			 TextView tv=new TextView(alertDialog.getContext());
			 tv.setText(message);
			 tv.setLayoutParams(LayoutUtil.getLayoutParamsFillParent());
			 alertDialog.setView(tv,KViewSpacingLeft,KViewSpacingTop,KViewSpacingRight,KViewSpacingBottom);
		 }
	 }
	 
	 public void setContentView(View contentView)
	 {
		 if(alertDialog==null)
	     {
			 alertDialog=builder.create();
	     }
		 
		 alertDialog.setView(contentView,KViewSpacingLeft,KViewSpacingTop,KViewSpacingRight,KViewSpacingBottom);
		 //alertDialog.setContentView(contentView);
	 }
	 
	 public void setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener) 
	 {
		 builder.setMultiChoiceItems(items, checkedItems, listener);
     }
	 
	 public void setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn, final OnMultiChoiceClickListener listener)
	 {
		 builder.setMultiChoiceItems(cursor, isCheckedColumn, labelColumn, listener);
     }
	  
	 public void addButton(String text)
	 {
		 addButton(text,this);
	 }
	 
	 public void addButtonOk()
	 {
		 String text=context.getString(android.R.string.ok);	
		 addButton(text,this);
	 }
	 
	 public void addButtonCancel()
	 {
		 String text=context.getString(android.R.string.cancel);	
		 addButton(text,this);
	 }
	 
	 public void addButton(String text,OnClickListener listener)
	 {
		 if(alertDialog==null)
	     {
			 alertDialog=builder.create();
	     }
		 
		 if(buttonCount<3)
		 {			 
			 int whichButton=getWhichButton(buttonCount);
			 alertDialog.setButton(whichButton,text,listener);
			 
			 buttonCount++;
		 }
	 }
	 
	 private int getWhichButton(int index)
	 {
		 int whichButton=0;
		 //只支锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷钮
		 switch(index)
		 {
			 case 0:
				 {
					 whichButton=DialogInterface.BUTTON_POSITIVE;
				 }
				 break;
			 case 1:
				 {
					 whichButton=DialogInterface.BUTTON_NEGATIVE;
				 }
				 break;
			 case 2:
				 {
					 whichButton=DialogInterface.BUTTON_NEUTRAL;
				 }
				 break;
		 }
		 
		 return whichButton;
	 }
	 
	 private int getButtonIndex(int whichButton)
	 {
		 int index=0;
		 //只支锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷钮
		 switch(whichButton)
		 {
			 case DialogInterface.BUTTON_POSITIVE:
				 {
					 index=0;
				 }
				 break;
			 case DialogInterface.BUTTON_NEGATIVE:
				 {
					 index=1;
				 }
				 break;
			 case DialogInterface.BUTTON_NEUTRAL:
				 {
					 index=2;
				 }
				 break;
		 }
		 
		 return index;
	 }
	 
//	 /**
//	  * 锟斤拷锟矫简单的对伙拷锟斤拷锟斤拷锟斤拷
//	  * @param message
//	  * @param title
//	  * @param addOkBtn
//	  */
//	 public void setDialogContent(String message,String title,boolean addOkBtn)
//	 {
//		 if(title!=null)
//			 alertDialog.setTitle(title);
//		 
//		 if(message!=null)
//		 {
//			 TextView tv=new TextView(alertDialog.getContext());
//			 tv.setText(message);
//			 //tv.setPadding(15, 5, 15, 5);
//			 tv.setLayoutParams(LayoutUtil.getLayoutParamsFillParent());
//			 alertDialog.setView(tv,25, 10, 25, 10);
//		 }
//		 
//		 if(addOkBtn)
//		 {
//			 alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"确锟斤拷",this);
//		 }
//		 
//	 }
	 
	 /**
	  * 锟斤拷锟矫革拷锟接的对伙拷锟斤拷锟斤拷锟斤拷
	  * @param content
	  */
	 public void setDialogContent(DialogContent content)
	 {		 
		 if(alertDialog==null)
	     {
		   alertDialog=builder.create();
	     }
		 
		 //title
		 alertDialog.setTitle(content.getTitle());
		 
		 //content
		 View view=content.getContentView();
		 if(view!=null)
		 {
			 alertDialog.setView(view);
		 }
	 }
	 
	 public void show()
	 { 
		 if(alertDialog==null)
	     {
		   alertDialog=builder.create();
	     }
		 
		 alertDialog.setCancelable(false);
		 
		 if(!alertDialog.isShowing())
		 {
			 alertDialog.show();
		 }
	 }
	 
	@SuppressWarnings("static-access")
	public int showWithBlock() 
	{
		return showWithBlock(null);
	}

	@SuppressWarnings("static-access")
	public int showWithBlock(View focusView) 
	{
		handler = new Handler() 
		{
			@Override
			public void handleMessage(Message mesg) 
			{
				// process incoming messages here
				// super.handleMessage(msg);
				throw new RuntimeException();
			}
		};

		show();

		if(focusView!=null)
		{
			 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
	         InputMethodManager inputMethodManager = (InputMethodManager) alertDialog.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
	         inputMethodManager.showSoftInput(focusView, 0); //锟斤拷示锟斤拷锟斤拷锟�
	         inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); //锟斤拷示锟斤拷锟斤拷锟�
		}
		
		try 
		{
			Looper.getMainLooper().loop();
		} 
		catch (RuntimeException e2) 
		{
		}

		return clickedIndex;
	}
	
	 /**
	  * from DialogInterface.OnClickListener
	  */
	public void onClick(DialogInterface dialog, int which)
	{
		clickedIndex=this.getButtonIndex(which);
		
		alertDialog.dismiss();  
		if(handler!=null)
		{
			Message m = handler.obtainMessage();  
			handler.sendMessage(m);  
		}
	}
	 
	public boolean isShowing()
	{
		return alertDialog.isShowing();
	}
	
	/**
	 * 锟斤拷锟斤拷alertDialog.cancel();锟斤拷氐锟�OnCancelListener
	 */
	public void hide()
	{
		alertDialog.hide();
	}
	
	/**
	 * 锟斤拷锟斤拷alertDialog.cancel();锟斤拷氐锟�OnCancelListener
	 */
	public void cancel()
	{
		alertDialog.cancel();
		simpleDialogContent=null;
	}
	
	public void setOnCancelListener(OnCancelListener listener)
	{
		alertDialog.setOnCancelListener(listener);
	}
	
	public void moveToBottom()
	{
		Window w=alertDialog.getWindow();
		//w.setBackgroundDrawableResource(rc);
		WindowManager.LayoutParams wl = w.getAttributes();
		
        int x=wl.x;
        int y=wl.y;
        
        wl.x=x;
        //wl.y=y+500;//锟斤拷锟斤拷锟狡筹拷锟斤拷幕
        wl.y=y+50;
        
        w.setAttributes(wl);
	}
	
	private void setSimpleDialogText(String text)
	{
		if(simpleDialogContent==null)
		{
			simpleDialogContent=new DialogContent();
			////dialogContent.setMessage("锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷...");
			TextView dialogView=new TextView(alertDialog().getContext());
			dialogView.setGravity(Gravity.LEFT);
			dialogView.setText(text);
			dialogView.setPadding(25, 10, 25, 10);
			dialogView.setLayoutParams(LayoutUtil.getLayoutParamsFillParent());
			simpleDialogContent.setContentView(dialogView);
			setDialogContent(simpleDialogContent);
			moveToBottom();
		}
		else
		{
			((TextView)simpleDialogContent.getContentView()).setText(text);
		}
	}
	
	static public void showSimpleDialog(Context context,String text,int icon)
	{
		CustomDialog dialog=new CustomDialog(context);
		dialog.setIcon(icon);
		dialog.setSimpleDialogText(text);
		dialog.show();
	}
	
	static public void showSimpleDialogWithBlock(Context context,String text)
	{
		CustomDialog dialog=new CustomDialog(context);
		dialog.setSimpleDialogText(text);
		dialog.showWithBlock();
	}
}
